package com.markyao.service.raft;

import com.markyao.aop.annotations.BroadcastCheck;
import com.markyao.utils.EncoderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@BroadcastCheck
@Component
public class BroadcastRegistService extends BroadcastService{
    private final String routeRegistAppName="/recos/group/registAppName";
    private final String routeRegistUrl="/recos/group/registUrl";
    private final String routeRegistWeight="/recos/group/registWeight";
    private final String routeRegistServer0="/recos/group/registServer0";
    private final String routeRegistRefrence="/recos/group/registRefrence";
    private final String routeRegistConnect="/recos/group/registConnect";
    private final String routeRegistAlive="/recos/group/registAlive";

    public boolean registAppName(String appName, HttpServletRequest request){
        String route=routeRegistAppName+"?appName="+appName;
        return generalBroadcast(route,"registAppName与{}通信失败!");
    }

    public boolean registUrl(String appName, String url) {
        String encodeUrl= EncoderUtils.encode(url);
        String route=routeRegistUrl+"?appName="+appName+"&url="+encodeUrl;
        return generalBroadcast(route,"registUrl与{}通信失败!");
    }

    public boolean registWeight(String appName, Integer weight){
        String route=routeRegistWeight+"?appName="+appName+"&weight="+weight;
        return generalBroadcast(route,"registWeight与{}通信失败!");
    }

    public boolean registServer0(String appName, String interfaceNAME, String clazz){
        String route=routeRegistServer0+"?appName="+appName+"&interfaceNAME="+interfaceNAME+"&clazz="+clazz;
        return generalBroadcast(route,"registServer0与{}通信失败!");
    }

    public boolean registRefrence(String appName, String refrenceName){
        String route=routeRegistRefrence+"?appName="+appName+"&refrenceName="+refrenceName;
        return generalBroadcast(route,"registRefrence与{}通信失败!");
    }

    public boolean registConnect(String appName, String targetUrl){
        String route=routeRegistConnect+"?appName="+appName+"&targetUrl="+targetUrl;
        return generalBroadcast(route,"registConnect与{}通信失败!");
    }

    public boolean registAlive(String appName) {
        String route=routeRegistAlive+"?appName="+appName;
        return generalBroadcast(route,"registAlive与{}通信失败!");
    }

}
